/**
 * 成品和半成品的转厂入口控制
 * 
 * 新品，量产发布，正在工作开启入口
 */
package ext.tianma.part.validators;

import java.util.Locale;

import wt.access.AccessControlHelper;
import wt.access.AccessPermission;
import wt.fc.WTObject;
import wt.fc.WTReference;
import wt.lifecycle.State;
import wt.org.WTPrincipal;
import wt.org.WTUser;
import wt.part.WTPart;
import wt.part.WTPartMaster;
import wt.session.SessionHelper;
import wt.type.TypedUtility;
import wt.util.WTException;

import com.ptc.core.meta.common.TypeIdentifier;
import com.ptc.core.ui.validation.DefaultUIComponentValidator;
import com.ptc.core.ui.validation.UIValidationCriteria;
import com.ptc.core.ui.validation.UIValidationKey;
import com.ptc.core.ui.validation.UIValidationResult;
import com.ptc.core.ui.validation.UIValidationResultSet;
import com.ptc.core.ui.validation.UIValidationStatus;

import ext.com.core.CoreUtil;



public class TransitFactoryValidator extends DefaultUIComponentValidator{
	
	public UIValidationResultSet performFullPreValidation(UIValidationKey uivalidationkey, UIValidationCriteria uivalidationcriteria, Locale locale) throws WTException {
		UIValidationStatus uivalidationstatus = UIValidationStatus.HIDDEN;
		UIValidationResultSet uivresultSet = new UIValidationResultSet();
		boolean flag = false;
		
		if (uivalidationcriteria != null && uivalidationcriteria.getContextObject() != null) {
			WTReference wtreference = uivalidationcriteria.getContextObject();
			if(null == wtreference)
			{
				return uivresultSet;
			}
			wt.fc.Persistable persistable = wtreference.getObject();
			
			WTUser curUser = (WTUser) SessionHelper.manager.getPrincipal();
			//WTPart part = (WTPart) persistable;
			WTPart part = null;
			if(persistable instanceof WTPartMaster)
			{
				WTPartMaster master = (WTPartMaster) persistable;
				part = CoreUtil.getWTPartByMasterAndView(master, "Design");
			}
			
			if(persistable instanceof WTPart)
			{
				
				part = (WTPart) persistable;
			}
			
			if(isObjectType(part,"SemiFinishedGood")||isObjectType(part,"C4")||isObjectType(part,"CF")||isObjectType(part,"FinishedGood"))
			{
				State state = part.getLifeCycleState();
				if( state != null ){
					String partState = state.toString();
					System.out.println( "Debug   partState :" + partState );
					if(partState.equals("NewProduct") || partState.equals("MassProductionRelease") || partState.equals("INWORK")|| partState.equals("TrialProduction")|| partState.equals("RiskOrder")|| partState.equals("CustomerSample") ){
						String number = part.getNumber();
						//编号以“-T”结尾的成品不做转厂
						if( !number.endsWith("-T"))
						{
//							String viewStr = part.getViewName();
//							if(viewStr.equals("Design"))
//							{
							WTPrincipal principal =  SessionHelper.manager.getPrincipal();
							
							if( principal != null ){
								
								//判断角色对对象是否有完全控制权限,如果有完全控制权限则可以修改“物料详细描述”
								boolean accessControl = AccessControlHelper.manager.hasAccess( principal , part, AccessPermission.ALL );
								
								if( accessControl ){
									flag = true;
								}
							}
//							}							
						}
					}
				}
				
			}
		}
		if (flag) {
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.ENABLED));
		} else {
			uivresultSet.addResult(new UIValidationResult(uivalidationkey, UIValidationStatus.HIDDEN));
		}
		return uivresultSet;
	}

	/**
	 *判断对象类型是否为指定的内部关键字类型
	 * @param object 对象；
	 * @param object 内部关键字；
	 * @return boolean  是指定的类型返回true；不是返回false
	 */
	public static boolean isObjectType(WTObject object,String objectType)
	{
		if(objectType.equals("") || objectType==null||object==null)
			return false;
		TypeIdentifier typeidentifier = TypedUtility.getTypeIdentifier(object);
		String typeName= typeidentifier.getTypename();
		if(typeName.indexOf(objectType)>=0)
		{
			return true;
		}
		return false;
	}
}
